﻿using MS_Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wanzheng.MS_UI
{
    public partial class Frm_ModVar_Mod : Form
    {
        public Frm_ModVar_Mod()
        {
            InitializeComponent();

        }

        public Frm_ModVar_Mod(Variable_Modbus ModVar, VarAlarm_Modbus ModAlarm)
        {
            InitializeComponent();
            this.var=ModVar;
            this.varAlarm=ModAlarm;
            this.Load += Frm_ModVar_Mod_Load;
        }

        public Variable_Modbus var;
        public VarAlarm_Modbus varAlarm;



        private void btn_OK_Click(object sender, EventArgs e)
        {
              

            //modbus变量实例

            var = new Variable_Modbus();
            var.VarName = this.txt_VarName.Text.Trim();
            var.StoreArea = this.cmb_StoreArea.Text.Trim();
            var.DataType = this.cmb_DataType.Text.Trim();
            var.Address = this.txt_Address.Text.Trim();
            var.IsFiling = this.chk_IsFiling.Checked ? "1" : "0";
            var.IsAlarm = this.chk_IsAlarm.Checked ? "1" : "0";
            var.IsReport = this.chk_IsReport.Checked ? "1" : "0";
            var.Note = this.txt_Note.Text.Trim();

            //modbus变量报警实例


            if (this.chk_High.Checked | this.chk_HiHi.Checked | this.chk_LoLo.Checked | this.chk_Low.Checked)
            {

                varAlarm = new VarAlarm_Modbus();
                varAlarm.VarName = this.txt_VarName.Text.Trim();


                if (chk_High.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_LoLo.Text.Trim()),
                        AlarmType = "High",
                        AlarmVaule = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                        Note = this.txt_Note_High.Text.Trim(),

                    });


                }

                if (chk_HiHi.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_HiHi.Text.Trim()),
                        AlarmType = "HiHi",
                        AlarmVaule = float.Parse(this.txt_Alarm_HiHi.Text.Trim()),
                        Note = this.txt_Note_HiHi.Text.Trim(),

                    });


                }

                if (chk_Low.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_Low.Text.Trim()),
                        AlarmType = "Low",
                        AlarmVaule = float.Parse(this.txt_Alarm_Low.Text.Trim()),
                        Note = this.txt_Note_Low.Text.Trim(),

                    });


                }


                if (chk_LoLo.Checked)
                {

                    varAlarm.VarAlarm.Add(new Alarm()
                    {
                        Priority = Convert.ToInt32(this.txt_Priority_LoLo.Text.Trim()),
                        AlarmType = "LoLo",
                        AlarmVaule = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                        Note = this.txt_Note_LoLo.Text.Trim(),

                    });


                }

            }


            this.DialogResult = DialogResult.OK;
            this.Close();


        }
        

        private void Frm_ModVar_Mod_Load(object sender, EventArgs e)
        {
            InitializeComponent();
            GetVaniable();
           
        }


        //加载变量值
        private void GetVaniable()
        {
          this.txt_VarName.Text=this.var.VarName;
            this.txt_Address.Text = this.var.Address;
            this.txt_Note.Text = this.var.Note;
            this.cmb_DataType.Text=this.var.DataType;
            this.cmb_StoreArea.Text=this.var.StoreArea;
            this.chk_IsFiling.Checked = this.var.IsFiling == "1" ? true : false;
            this.chk_IsAlarm.Checked = this.var.IsAlarm== "1" ? true : false;
            this.chk_IsReport.Checked = this.var.IsReport== "1" ? true : false;
            if (this.varAlarm.VarAlarm.Count > 0)
            {
                foreach (Alarm item in this.varAlarm.VarAlarm)
                {
                    switch (item.AlarmType)
                    {
                        case "High":
                            this.txt_Alarm_High.Text=item.AlarmVaule.ToString();
                            this.txt_Priority_High.Text = item.Priority.ToString();
                            this.txt_Note_High.Text = item.Note.ToString();
                            this.chk_High.Checked = true;

                            //case必须有break返回
                            break;
                        case "HiHi":


                            this.txt_Alarm_HiHi.Text = item.AlarmVaule.ToString();
                            this.txt_Priority_HiHi.Text = item.Priority.ToString();
                            this.txt_Note_HiHi.Text = item.Note.ToString();
                            this.chk_HiHi.Checked = true;
                            break;
                        case "Low":
                            this.txt_Alarm_Low.Text = item.AlarmVaule.ToString();
                            this.txt_Priority_Low.Text = item.Priority.ToString();
                            this.txt_Note_Low.Text = item.Note.ToString();
                            this.chk_Low.Checked = true;
                            break;
                        case "LoLo":


                            this.txt_Alarm_LoLo.Text = item.AlarmVaule.ToString();
                            this.txt_Priority_LoLo.Text = item.Priority.ToString();
                            this.txt_Note_LoLo.Text = item.Note.ToString();
                            this.chk_LoLo.Checked = true;
                            break;
                            default:
                            break;  


                    }
                
                }
            }
        }

        private void InitialComboBox()
        {

            //初始化ComboBox下拉菜单

            //1类型
            this.cmb_DataType.Items.Add("Bool");
            this.cmb_DataType.Items.Add("Signed");
            this.cmb_DataType.Items.Add("Unsigned");
            this.cmb_DataType.Items.Add("Hex");
            this.cmb_DataType.Items.Add("Long");
            this.cmb_DataType.Items.Add("Long Inverse");
            this.cmb_DataType.Items.Add("Float");
            this.cmb_DataType.Items.Add("Float Inverse");
            this.cmb_DataType.Items.Add("Double");
            this.cmb_DataType.Items.Add("Double Inverse");
            this.cmb_DataType.SelectedIndex = 6;



            //存储区
            this.cmb_StoreArea.Items.Add("01 Coil Status(0X)");
            this.cmb_StoreArea.Items.Add("02 Input Status(1X)");
            this.cmb_StoreArea.Items.Add("03 Holding Register(4X)");
            this.cmb_StoreArea.Items.Add("04 Input Registers(3X)");
            this.cmb_StoreArea.SelectedIndex = 2;


            //初始化时，下面勾选框都是禁用的


            this.chk_High.Enabled = false;
            this.chk_HiHi.Enabled = false;
            this.chk_Low.Enabled = false;
            this.chk_LoLo.Enabled = false;

        }


        #region 变化实践
        private void chk_IsAlarm_CheckedChanged(object sender, EventArgs e)
        {
            //报警勾选了才能用这几个选项功能

            bool value = this.chk_IsAlarm.Checked;
            this.chk_High.Enabled = value;
            this.chk_HiHi.Enabled = value;
            this.chk_Low.Enabled = value;
            this.chk_LoLo.Enabled = value;


        }




        //勾选了高中低的做法
        private void chk_LoLo_CheckedChanged(object sender, EventArgs e)
        {

            bool value = this.chk_LoLo.Checked;
            txt_Alarm_LoLo.Enabled = value;
            txt_Priority_LoLo.Enabled = value;
            txt_Note_LoLo.Enabled = value;


        }

        private void chk_Low_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_Low.Checked;
            txt_Alarm_Low.Enabled = value;
            txt_Priority_Low.Enabled = value;
            txt_Note_Low.Enabled = value;
        }

        private void chk_HiHi_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_HiHi.Checked;
            txt_Alarm_HiHi.Enabled = value;
            txt_Priority_HiHi.Enabled = value;
            txt_Note_HiHi.Enabled = value;
        }

        private void chk_High_CheckedChanged(object sender, EventArgs e)
        {
            bool value = this.chk_High.Checked;
            txt_Alarm_High.Enabled = value;
            txt_Priority_High.Enabled = value;
            txt_Note_High.Enabled = value;
        }

        #endregion

    }
}
